﻿using ModuleBase.googoltech.GTS;
using ModuleBase;
using System.Diagnostics;
using Prism.Ioc;

namespace AxisStop
{
    public class AxisStop : ModuleBase.ModuleBase
    {
        private IAxisCardService _axisCardService;
        private ModuleStateService _moduleStateService;
        bool outputs = false;
        private IContainerProvider _containerProvider;

        private int _parameter1;
        public int Parameter1
        {
            get => _parameter1;
            set
            {
                if (_parameter1 != value)
                {
                    _parameter1 = value;
                    OnPropertyChanged(nameof(Parameter1));
                }
            }
        }

        private string _parameter2 = "Top_A8";
        public string Parameter2
        {
            get => _parameter2;
            set
            {
                if (_parameter2 != value)
                {
                    _parameter2 = value;
                    OnPropertyChanged(nameof(Parameter2));
                }
            }
        }
        public override Dictionary<string, object> GetProperties()
        {
            return new Dictionary<string, object>
            {
                { "轴号", Parameter1 },
                { "轴卡参数名", Parameter2 },
            };
        }

        public override Dictionary<string, List<object>> PropertyOptions { get; } = new Dictionary<string, List<object>>
        {
            { "轴号", new List<object>{ 1,2,3,4,5,6,7,8 } }
        };

        public AxisStop(IContainerProvider containerProvider, ModuleStateService moduleStateService)
        {
            _containerProvider = containerProvider;
            _moduleStateService = moduleStateService;
            _moduleStateService.RegisterModule(Id);
            Name = "轴停止";

            DisplayNameToPropertyNameMap = new Dictionary<string, string>
            {
                { "轴号", nameof(Parameter1) },
                { "轴卡参数名", nameof(Parameter2) },
            };
        }

        public override bool Execute()
        {
            Status = ModuleBase.ModuleStatus.Executing; // 设置状态为执行中
            _axisCardService = _containerProvider.Resolve<IAxisCardService>(Parameter2);
            AxisParm axisParm = GetAxisByNumber(Parameter1);
            try
            {
                GTSCard.AxisStop(axisParm, 0);
                outputs = true;
            }
            catch (Exception exp)
            {
                Debug.WriteLine($"模块[{Name}]执行异常：" + exp.ToString());
                outputs = false;
            }
            return outputs;
        }

        public override async Task<bool> ExecuteAsync()
        {
            outputs = false;
            var result = await Task.Run(() => Execute());
            return result;
        }

        public override void Initialize()
        {

        }
        private AxisParm GetAxisByNumber(int axisNumber)
        {
            switch (axisNumber)
            {
                case 1: return _axisCardService.A1;
                case 2: return _axisCardService.A2;
                case 3: return _axisCardService.A3;
                case 4: return _axisCardService.A4;
                case 5: return _axisCardService.A5;
                case 6: return _axisCardService.A6;
                case 7: return _axisCardService.A7;
                case 8: return _axisCardService.A8;
                default: return _axisCardService.A1;
            }
        }

        public override string Description => "轴停止模块：用于紧急或常规停止指定轴的运动。";
    }
}
